<template>
	<view>
		<cu-custom :bgColor="'bg-'+color" :isBack="true"><block slot="backText">返回</block><block slot="content">参数配置</block></cu-custom>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">I  D</view>
				<input placeholder="芯烨云平台注册用户名" v-model="from.user"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">密钥</view>
				<input placeholder="用户开发者密钥" v-model="from.user_key"></input>
				<button class='cu-btn bg-green shadow' :style="{'backgroundColor':btnColor}" data-label="user_key" @click="scan">扫码</button>
			</view>
			<view class="cu-form-group">
				<view class="title">编号</view>
				<input placeholder="打印机编号" v-model="from.sn"></input>
				<button class='cu-btn bg-green shadow' :style="{'backgroundColor':btnColor}" data-label="sn" @click="scan">扫码</button>
			</view>
		</form>
		<view class="padding flex flex-direction">
			<button class="cu-btn lg text-white" @click="save" :style="{'backgroundColor':btnColor}">确定</button>
		</view>
	</view>
</template>

<script>
	import Vue from 'vue'
	export default {
		data() {
			return {
				from:{
					user:'',
					user_key:'',
					sn:''
				},
				color: Vue.prototype.bgColor,
				btnColor:Vue.prototype.btnColor,
				
			};
		},
		created() {
			var that = this
			uni.getStorage({
				key:'y2config',
				success: (res) => {
					that.from = res.data;
					console.log(JSON.stringify(res))
				},
				error:(e) => {
					console.log(JSON.stringify(e))
				}
			});
		},
		methods: {
			save(){
				uni.setStorage({
					key:'y2config',
					data:this.from,
					success: (res) => {
						uni.showToast({
							title:'设置成功'
						})
						uni.navigateBack({
						})
					},
					fail: () => {
						uni.showToast({
							title:'设置失败'
						})
					}
				})
			},
			scan(e){
				var that = this
				let label = e.currentTarget.dataset.label
				uni.scanCode({
					success(res) {
						that.from[label] = res.result
					}
				})
			}
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
